package com.aletto.general_store.presenters;

import android.content.Context;
import android.content.Intent;

import androidx.recyclerview.widget.RecyclerView;

import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.ui.activities.SearchDetailsActivity;
import com.aletto.general_store.ui.popup.GoodsStoreSwitchPopup;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.QuickList;
import com.sisyphus.spinach.ui.configs.impl.DefaultTemplate;
import com.aletto.general_store.ui.HotSearchItemDecoration;
import com.aletto.general_store.ui.adapters.SearchHotAdapter;

import java.util.List;

/**
 * 热门搜索
 */
public class SearchHotTemplate extends DefaultTemplate<List<String>> {

    private final RecyclerView mRecyclerHotSearch;
    private       int          searchType = GoodsStoreSwitchPopup.GOODS_TYPE;

    public SearchHotTemplate(RecyclerView parent) {
        super(parent);
        mRecyclerHotSearch = parent;
    }

    @Override
    public INetTask.View<List<String>> view() {
        mRecyclerHotSearch.addItemDecoration(new HotSearchItemDecoration());
        SearchHotAdapter hotSearchAdapter = new SearchHotAdapter();

        hotSearchAdapter.setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            context.startActivity(SearchDetailsActivity.newIntent(
                            context,
                            hotSearchAdapter.getItem(position),
                            searchType
                    )
            );
        });

        mRecyclerHotSearch.setAdapter(hotSearchAdapter);
        return new QuickList<>(hotSearchAdapter);
    }

    public void setParams(int type) {
        this.searchType = type;
    }

    @Override
    public INetTask.Presenter<List<String>, INetTask.View<List<String>>> presenter() {
        return new NetSingleTask<>(DataRepository.getInstance().hotSearch());
    }

}
